

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>pyamp.web.http.argument &mdash; pyamp v1.2 documentation</title>
    <link rel="stylesheet" href="../../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../../',
        VERSION:     '1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    <link rel="top" title="pyamp v1.2 documentation" href="../../../../index.html" />
    <link rel="up" title="Module code" href="../../../index.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../../../index.html">pyamp v1.2 documentation</a> &raquo;</li>
          <li><a href="../../../index.html" accesskey="U">Module code</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for pyamp.web.http.argument</h1><div class="highlight"><pre>
<span class="c"># Copyright 2012 Brett Ponsler</span>
<span class="c"># This file is part of pyamp.</span>
<span class="c">#</span>
<span class="c"># pyamp is free software: you can redistribute it and/or modify</span>
<span class="c"># it under the terms of the GNU General Public License as published by</span>
<span class="c"># the Free Software Foundation, either version 3 of the License, or</span>
<span class="c"># (at your option) any later version.</span>
<span class="c">#</span>
<span class="c"># pyamp is distributed in the hope that it will be useful,</span>
<span class="c"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="c"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="c"># GNU General Public License for more details.</span>
<span class="c">#</span>
<span class="c"># You should have received a copy of the GNU General Public License</span>
<span class="c"># along with pyamp.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</span>
<span class="sd">&#39;&#39;&#39;The argument module contains the Argument class which provides</span>
<span class="sd">the ability to create and manage arguments that can be passed</span>
<span class="sd">through HTTP requests.</span>

<span class="sd">&#39;&#39;&#39;</span>


<div class="viewcode-block" id="Argument"><a class="viewcode-back" href="../../../../_generated/pyamp_web_http_argument.html#pyamp.web.http.argument.Argument">[docs]</a><span class="k">class</span> <span class="nc">Argument</span><span class="p">:</span>
    <span class="sd">&#39;&#39;&#39;The Argument class encapsulates the concept of an HTTP argument</span>
<span class="sd">    passed through a URL request.</span>

<span class="sd">    Arguments can have a specific type, can be required or optional,</span>
<span class="sd">    and can have a default value if they are optional and not passed in</span>
<span class="sd">    the request.</span>

<span class="sd">    &#39;&#39;&#39;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">argumentType</span><span class="p">,</span> <span class="n">required</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;</span>
<span class="sd">        * argumentType -- The type of the argument</span>
<span class="sd">        * required -- True if the argument is required, False otherwise</span>
<span class="sd">        * default -- The default value for optional Arguments</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__type</span> <span class="o">=</span> <span class="n">argumentType</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__required</span> <span class="o">=</span> <span class="n">required</span>

        <span class="c"># Ensure that the default value is the correct type</span>
        <span class="k">if</span> <span class="n">default</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="ow">and</span> <span class="nb">type</span><span class="p">(</span><span class="n">default</span><span class="p">)</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__getType</span><span class="p">():</span>
            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s">&quot;Default value must be type </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> \
                                <span class="bp">self</span><span class="o">.</span><span class="n">__getType</span><span class="p">()</span><span class="o">.</span><span class="n">__name__</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">__defaultValue</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">convertValue</span><span class="p">(</span><span class="n">default</span><span class="p">)</span>

<div class="viewcode-block" id="Argument.getDefaultValue"><a class="viewcode-back" href="../../../../_generated/pyamp_web_http_argument.html#pyamp.web.http.argument.Argument.getDefaultValue">[docs]</a>    <span class="k">def</span> <span class="nf">getDefaultValue</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Return the default value for this Argument.&#39;&#39;&#39;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__defaultValue</span>
</div>
<div class="viewcode-block" id="Argument.isRequired"><a class="viewcode-back" href="../../../../_generated/pyamp_web_http_argument.html#pyamp.web.http.argument.Argument.isRequired">[docs]</a>    <span class="k">def</span> <span class="nf">isRequired</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Determine if this is a required Argument or an optional Argument.&#39;&#39;&#39;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__required</span>
</div>
<div class="viewcode-block" id="Argument.convertValue"><a class="viewcode-back" href="../../../../_generated/pyamp_web_http_argument.html#pyamp.web.http.argument.Argument.convertValue">[docs]</a>    <span class="k">def</span> <span class="nf">convertValue</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Convert the given value to the expected type of the Argument.</span>

<span class="sd">        * value -- The value of the argument passed in the request</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">if</span> <span class="n">value</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">value</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="n">evaluated</span> <span class="o">=</span> <span class="nb">eval</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">value</span><span class="p">))</span>
        <span class="k">except</span> <span class="ne">NameError</span><span class="p">:</span>
            <span class="n">evaluated</span> <span class="o">=</span> <span class="n">value</span>

        <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">evaluated</span><span class="p">)</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__getType</span><span class="p">():</span>
            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s">&quot;Argument expected type &#39;</span><span class="si">%s</span><span class="s">&#39; but found &quot;</span> \
                                <span class="s">&quot;type &#39;</span><span class="si">%s</span><span class="s">&#39;&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__getType</span><span class="p">()</span><span class="o">.</span><span class="n">__name__</span><span class="p">,</span>
                                               <span class="nb">type</span><span class="p">(</span><span class="n">evaluated</span><span class="p">)</span><span class="o">.</span><span class="n">__name__</span><span class="p">))</span>

        <span class="c"># The types match, return the converted value</span>
        <span class="k">return</span> <span class="n">evaluated</span>

    <span class="c">##### Private Functions #####</span>
</div>
    <span class="k">def</span> <span class="nf">__getType</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Get the type of this Argument.&#39;&#39;&#39;</span>
        <span class="k">return</span> <span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__type</span><span class="p">())</span>

</div>
<div class="viewcode-block" id="IntArgument"><a class="viewcode-back" href="../../../../_generated/pyamp_web_http_argument.html#pyamp.web.http.argument.IntArgument">[docs]</a><span class="k">class</span> <span class="nc">IntArgument</span><span class="p">(</span><span class="n">Argument</span><span class="p">):</span>
    <span class="sd">&#39;&#39;&#39;The IntArgument class provides a class to easily create</span>
<span class="sd">    an integer argument.</span>

<span class="sd">    &#39;&#39;&#39;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">required</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;</span>
<span class="sd">        * required -- True if the argument is required, False otherwise</span>
<span class="sd">        * default -- The default value for optional Arguments</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="n">Argument</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">int</span><span class="p">,</span> <span class="n">required</span><span class="p">,</span> <span class="n">default</span><span class="p">)</span>

</div>
<div class="viewcode-block" id="FloatArgument"><a class="viewcode-back" href="../../../../_generated/pyamp_web_http_argument.html#pyamp.web.http.argument.FloatArgument">[docs]</a><span class="k">class</span> <span class="nc">FloatArgument</span><span class="p">(</span><span class="n">Argument</span><span class="p">):</span>
    <span class="sd">&#39;&#39;&#39;The FloatArgument class provides a class to easily create</span>
<span class="sd">    an float argument.</span>

<span class="sd">    &#39;&#39;&#39;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">required</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;</span>
<span class="sd">        * required -- True if the argument is required, False otherwise</span>
<span class="sd">        * default -- The default value for optional Arguments</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="n">Argument</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">float</span><span class="p">,</span> <span class="n">required</span><span class="p">,</span> <span class="n">default</span><span class="p">)</span>

</div>
<div class="viewcode-block" id="BoolArgument"><a class="viewcode-back" href="../../../../_generated/pyamp_web_http_argument.html#pyamp.web.http.argument.BoolArgument">[docs]</a><span class="k">class</span> <span class="nc">BoolArgument</span><span class="p">(</span><span class="n">Argument</span><span class="p">):</span>
    <span class="sd">&#39;&#39;&#39;The BoolArgument class provides a class to easily create</span>
<span class="sd">    an boolean argument.</span>

<span class="sd">    &#39;&#39;&#39;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">required</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;</span>
<span class="sd">        * required -- True if the argument is required, False otherwise</span>
<span class="sd">        * default -- The default value for optional Arguments</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="n">Argument</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">bool</span><span class="p">,</span> <span class="n">required</span><span class="p">,</span> <span class="n">default</span><span class="p">)</span>

</div>
<div class="viewcode-block" id="StrArgument"><a class="viewcode-back" href="../../../../_generated/pyamp_web_http_argument.html#pyamp.web.http.argument.StrArgument">[docs]</a><span class="k">class</span> <span class="nc">StrArgument</span><span class="p">(</span><span class="n">Argument</span><span class="p">):</span>
    <span class="sd">&#39;&#39;&#39;The StrArgument class provides a class to easily create</span>
<span class="sd">    a string argument.</span>

<span class="sd">    &#39;&#39;&#39;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">required</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;</span>
<span class="sd">        * required -- True if the argument is required, False otherwise</span>
<span class="sd">        * default -- The default value for optional Arguments</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="n">Argument</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">str</span><span class="p">,</span> <span class="n">required</span><span class="p">,</span> <span class="n">default</span><span class="p">)</span></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../../../index.html">pyamp v1.2 documentation</a> &raquo;</li>
          <li><a href="../../../index.html" >Module code</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, Brett Ponsler.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.1.
    </div>
  </body>
</html>